package com.aier.service.inventory;

import com.aier.bean.inventory.InventoryBean;
import com.aier.bean.common.CommonResult;
import com.aier.bean.common.GlobalCode;
import com.aier.exception.CastException;
import com.aier.mapper.inventory.InventoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存service
 */
@Service
public class InventoryService {
    @Resource
    private InventoryMapper inventoryMapper;

    /**
     * 获取所有商品的库存信息
     *
     * @return
     */
    public List<InventoryBean> getAllInventoryInfo() {
        List<InventoryBean> result = new ArrayList<>();
        try {
            result = inventoryMapper.getAllInventoryInfo();
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.READ_INVENTORY_FAIL);
        }
        return result;
    }

    /**
     * 插入商品录入信息时先判断对应商品是否有库存表，没有库存则抛出异常
     *
     * @param modeName
     * @return
     */
    public List<InventoryBean> getAllInventoryInfoByModelName(String modeName) {
        List<InventoryBean> inventoryList = new ArrayList<>();
        try {
            inventoryList = inventoryMapper.getAllInventoryInfoByModelName(modeName);
            if (inventoryList.size() <= 0){
                CastException.cast(GlobalCode.NOT_INVENTORY);
            }
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.NOT_INVENTORY);
        }
        return inventoryList;
    }

    /**
     * 获取所有的型号列表
     *
     * @return
     */
    public CommonResult getAllModeName() {
        List<String> result = new ArrayList<>();
        try {
            result = inventoryMapper.getAllModeName();
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.READ_ALL_MODEL_NAME_FAIL);
        }
        return CommonResult.success(result);
    }


    /**
     * 修改业务员姓名
     */
    public CommonResult updateSalespersonByModelNameAndOldSalesperson(String modelName, String oldSalesperson, String newSalesperson) {
        try {
            int updateRows = inventoryMapper.updateSalespersonByModelNameAndOldSalesperson(modelName, oldSalesperson, newSalesperson);
            if (updateRows <= 0){
                CastException.cast(GlobalCode.UPDATE_SALESPERSON_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.UPDATE_SALESPERSON_FAIL);
        }
        return CommonResult.success();
    }


    /**
     * 插入商品库存信息
     * @param inventoryBean
     */
    public CommonResult saveInventory(InventoryBean inventoryBean){
        try {
            inventoryMapper.insert(inventoryBean);
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.SAVE_INVENTORY_FAIL);
        }
        return CommonResult.success();
    }

    /**
     * 获取所有业务员姓名
     * @return
     */
    public CommonResult getAllSalespersonName(){
        List<String> result = new ArrayList<>();
        try {
            result = inventoryMapper.getAllSalespersonName();
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.READ_ALL_SALESPERSON_NAME_FAIL);
        }
        return CommonResult.success(result);
    }


    /**
     * 判断录入的库存信息型号是否存在
     * @param modelName
     * @return
     */
    public boolean inventoryIsExist(String modelName){
        List<InventoryBean> inventoryBeanList = inventoryMapper.getAllInventoryInfoByModelName(modelName);
        if (inventoryBeanList.size()>0){
            CastException.cast(GlobalCode.INVENTORY_IS_EXIST);
        }
        return true;
    }

    /**
     * 判断商品型号是否是该业务员负责,不是则抛出异常
     * @return
     */
    public boolean inventoryIsExistByModelNameAndSalesperson(String modelName, String salesperson){
        List<InventoryBean> inventoryBeanList = new ArrayList<>();
        try {
            inventoryBeanList = inventoryMapper.getInventoryListByModelNameAndSalesperson(modelName, salesperson);
            if (inventoryBeanList.size() <= 0){
                CastException.cast(GlobalCode.SALESPERSON_NOT_RESPONSIBLE);
            }
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.SALESPERSON_NOT_RESPONSIBLE);
        }
        return true;
    }
}
